package com.xiao.singleapp.controller;

import com.xiao.singleapp.service.GenCodeService;
import com.xiao.singleapp.service.impl.GenCodeServiceImpl;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @CreateBy 花和尚  1130365579@qq.com  2023/3/18 22:34
 * @Version 1.0
 */
@Controller
@RequestMapping("/genCode")
public class SingleAppCodeGenController {

    private final GenCodeService genCodeService = new GenCodeServiceImpl();


    /**
     * 为指定表（数组，多个表）生成代码
     * http://localhost:8087/genCode/specifiedTable？tableNames=table1,table2,table3&packageName=包名
     *
     * @param request     http 请求
     * @param response    http 响应
     * @param tableNames  表名称数组
     * @param packageName 包名
     * @throws IOException
     */
    @GetMapping("/specifiedTable")
    @ResponseBody
    public void test1(HttpServletRequest request,
                      HttpServletResponse response,
                      @RequestParam("tableNames") String[] tableNames,
                      @RequestParam("packageName") String packageName) throws IOException {

        final byte[] data = genCodeService.generatorCode(tableNames, packageName);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"genCode.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

    /**
     * 为所有表生成代码
     * http://localhost:8087/genCode/allTabsGenCode/包名
     */
    @GetMapping("/allTabsGenCode/{packageName}")
    @ResponseBody
    public void allTabsGenCode(HttpServletRequest request,
                               HttpServletResponse response,
                               @PathVariable("packageName") String packageName) throws IOException {
        List<String> tableNameList = genCodeService.listAllTableName();
        String[] tableNames = new String[tableNameList.size()];
        tableNameList.toArray(tableNames);
        final byte[] data = genCodeService.generatorCode(tableNames, packageName);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"genCode.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(data, response.getOutputStream());
    }

    @GetMapping("/index")
    public String index(Model model) {
        model.addAttribute("title", "代码生成器");
        return "index";
    }

    @GetMapping("/listAllTabs")
    public String listAllTabs(Model model) {
        List<Map<String, String>> tabs = genCodeService.listAllTabInfo();
        model.addAttribute("allTabs", tabs);
        return "index";
    }


}
